package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.chenfeng.vo.SearchIndexVo;
import com.cskaoyan.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 搜索部分的controller
 *
 * @author Chen_Feng
 * @since 2022/06/29 16:25
 */
@RequestMapping("wx/search")
@RestController("wxSearchController")
public class SearchController {

    @Autowired
    SearchService searchService;

    /**
     * 获取热词、默认词以及历史搜索
     *
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.SearchIndexVo>
     * @author Chen_Feng
     * @since 2022/06/29 16:36
     */
    @RequestMapping("index")
    public BaseRespVo<SearchIndexVo> index() {

        // 执行业务操作
        try {
            SearchIndexVo data = searchService.getSearchIndex();
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("真的空空如也？");
        }
    }

    /**
     * 删除历史搜索的关键词
     *
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.SearchIndexVo>
     * @author Chen_Feng
     * @since 2022/06/29 17:47
     */
    @RequestMapping("clearhistory")
    public BaseRespVo<SearchIndexVo> clearHistory() {

        // 执行业务操作
        try {
            boolean flag = searchService.clearHistory();
            if (flag) {
                return BaseRespVo.ok(null);
            }
            return BaseRespVo.invalidParameter("已经很干净了，无需清除");
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("删不掉耶");
        }
    }

    /**
     * 用户输入关键字时即使查询匹配的关键词
     *
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.SearchIndexVo>
     * @author Chen_Feng
     * @since 2022/06/29 18:05
     */
    @RequestMapping("helper")
    public BaseRespVo<String> helper(String keyword) {

        // 执行业务操作
        try {
            List<String> data = searchService.getHelper(keyword);
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("真的空空如也？");
        }
    }
}
